﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using AdminApp.Controls;
using AdminApp.Web.Services;
using System.IO;
using AdminApp.Resources;
using System.Linq;

namespace AdminApp.Views
{
    public partial class Speaker : Page
    {
        public Speaker()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void speakerDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void _add_Click(object sender, RoutedEventArgs e)
        {
            DigitalSignageDomainContext context = 
                speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;
            
            Web.Speaker speaker = new Web.Speaker();
            speaker.Lastname = "Lastname";
            speaker.Firstname = "Firstname";
            speaker.Title = "Speaker";
            speaker.Biography = "Speaker Bio";

            context.Speakers.Add(speaker);
            context.SubmitChanges();
        }

        private void _delete_Click(object sender, RoutedEventArgs e)
        {
            if (_speakerList.SelectedItem != null)
            {
                QuestionWindow qw = 
                    new QuestionWindow(ApplicationStrings.DeleteSpeakerQuestion, DeleteSpeaker);
                qw.Show();
            }
        }

        private void DeleteSpeaker()
        {
            DigitalSignageDomainContext context = 
                speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;

            Web.Speaker speaker = _speakerList.SelectedItem as Web.Speaker;
            context.Speakers.Remove(speaker);
        }

        private void _selectSpeakerImage_Click(object sender, RoutedEventArgs e)
        {
            SelectImage();
        }

        private void SelectImage()
        {
            Web.Speaker currentSpeaker = _speakerList.SelectedItem as Web.Speaker;
            if (currentSpeaker != null)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                var result = ofd.ShowDialog();
                if (result.HasValue && result.Value == true)
                {
                    FileInfo fi = ofd.File;
                    var stream = fi.OpenRead();
                    int streamLength = (int)stream.Length;
                    byte[] bytes = new byte[streamLength];
                    stream.Read(bytes, 0, streamLength);
                    currentSpeaker.ImageData = bytes;
                }
            }
        }

        private void SearchControl_SearchTextChanged(object sender, AdminApp.Controls.SearchEventArgs args)
        {
            DigitalSignageDomainContext ctx = speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;
            if (args.SearchText.Length > 0)
            {
                var result = from s in ctx.Speakers
                             where s.Lastname.ToLower().Contains(args.SearchText.ToLower()) ||
                                s.Firstname.ToLower().Contains(args.SearchText.ToLower())
                             select s;
                _speakerList.ItemsSource = result;
            }
            else
            {
                _speakerList.ItemsSource = ctx.Speakers;
            }
        }
    }
}
